local args = ngx.req.get_uri_args()

if nil == args.item_id or nil == args.act_id then
    ngx.say('{"code":10000, "message":"act_id和item_id不能为空"}')
    return
end

local act_id = args.act_id
local item_id = args.item_id

local redis_client = require "lib.redis"
local redis = redis_client:new()
local item, err = redis:get(string.format('_item:%s:%s', act_id, item_id))

if err ~= nil then
    ngx.log(ngx.ERR, 'connect redis faild:', err)
    ngx.say('{"code":10000,"message":"服务器内部错误","error":"', err,'"}')
    return
end

if item == nil then
    ngx.say('{"code":10000,"message":"投票项不存在"}')
    return
end

local ranking_key = 'ranking:'..act_id
local votes = redis:zscore(ranking_key, item_id) or 0
local rank = redis:zrevrank(ranking_key, item_id) or 0
votes = tonumber(votes)
rank = tonumber(rank) + 1

ngx.say('{"data":{"item":', item,',"votes":', votes,',"rank":', rank,'},"code":200,"message":"success"}')
